﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "SingleGridUiManipulateStrategy.h"
#include "IElement.h"
#include "IModelView.h"
#include "IElementShapeHandle.h"
#include "IGenericElement.h"
#include "IElementBasicInformation.h"
#include "GbmpModelViewTypes.h"
#include "ElementUiManipulateStrategyRegister.h"
#include "SingleGridBehaviors.h"
#include "SingleGrid.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

namespace
{

    Vector3d GetControlPoint(const SingleGrid* pGrid, GridControlPointType type, ElementId modelviewId)
    {
        DBG_WARN_AND_RETURN_UNLESS(pGrid, Vector3d::Zero, L"pGrid为空",L"GDMPLab",L"2024-03-30");

        Vector3d ret;
        switch (type)
        {
        case GridControlPointType::HorizontalStart:
            ret = pGrid->GetHorizontalStartPoint(modelviewId);
            break;
        case GridControlPointType::HorizontalEnd:
            ret = pGrid->GetHorizontalEndPoint(modelviewId);
            break;
        case GridControlPointType::VerticalStart:
            ret = pGrid->GetVerticalStartPoint(modelviewId);
            break;
        case GridControlPointType::VerticalEnd:
            ret = pGrid->GetVerticalEndPoint(modelviewId);
            break;
        default:
            break;
        }

        return ret;
    }
}

std::vector<ElementId> gcmp::SingleGridUiManipulateStrategy::CreateAuxiliaryElements(gcmp::IModelView* pModelView, const IElement* pElement) const
{
    std::vector<ElementId> res;
    DBG_WARN_AND_RETURN_UNLESS(pElement, res, L"m_pElement为空",L"GDMPLab",L"2024-03-30");
    DBG_WARN_AND_RETURN_UNLESS(pModelView != nullptr, res, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    bool isGridVisible = pElement->GetViewSpecificShapeComponent() == nullptr ? true : pElement->GetViewSpecificShapeComponent()->IsVisibleInModelView(pModelView);
    if (!isGridVisible)
    {
        return res;
    }

    if (PlanViewType == pModelView->GetUserDataId())

    {
        res.push_back(CreateGripPoint(GridControlPointType::HorizontalStart, pModelView->GetElementId(), pElement));
        res.push_back(CreateGripPoint(GridControlPointType::HorizontalEnd, pModelView->GetElementId(), pElement));
    }
    else if (ElevationViewType == pModelView->GetUserDataId())

    {
        res.push_back(CreateGripPoint(GridControlPointType::VerticalStart, pModelView->GetElementId(), pElement));
        res.push_back(CreateGripPoint(GridControlPointType::VerticalEnd, pModelView->GetElementId(), pElement));
    }
    else if (SectionViewType == pModelView->GetUserDataId())

    {
        res.push_back(CreateGripPoint(GridControlPointType::VerticalStart, pModelView->GetElementId(), pElement));
        res.push_back(CreateGripPoint(GridControlPointType::VerticalEnd, pModelView->GetElementId(), pElement));
    }
    return res;
}

gcmp::ElementId gcmp::SingleGridUiManipulateStrategy::CreateGripPoint(GridControlPointType type, const gcmp::ElementId& modelviewId, const IElement* pElement) const
{
    const IGenericElement* pGenericElement = quick_cast<IGenericElement>(pElement);
    DBG_WARN_AND_RETURN_UNLESS(pGenericElement, ElementId::InvalidID, L"pGenericElement为空。",L"GDMPLab",L"2024-03-30");
    const SingleGrid* pGrid = quick_cast<SingleGrid>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_UNLESS(pGrid, ElementId::InvalidID, L"pGrid为空。",L"GDMPLab",L"2024-03-30");

    OwnerPtr<IElementShapeHandleBehavior> pGripPointBehavior = SingleGridGripPointBehavior::Create(pElement->GetDocument(), modelviewId);
    //std::vector<Int64> snapPriorityTypes;
    //snapPriorityTypes.emplace_back(typeid(SingleGrid).hash_code());
    //pGripPointBehavior->SetSnapPriorityTypes(snapPriorityTypes);
    Vector3d pos = GetControlPoint(pGrid, type, modelviewId);

    IElementShapeHandle* pGripPoint = IElementShapeHandle::Create(
        TransferOwnership(pGripPointBehavior),
        pGenericElement->GetElementId(),
        pos, static_cast<int>(type));
    DBG_WARN_AND_RETURN_UNLESS(pGripPoint != nullptr, ElementId::InvalidID, L"pGripPoint为空",L"GDMPLab",L"2024-03-30");



    return pGripPoint->GetElementId();

}

REGISTER_ELEMENT_UIMANIPULATE_STRATEGY(SingleGrid, SingleGridUiManipulateStrategy);
